//
//  TabBarViewController.m
//  MS
//
//  Created by He Yuan on 13-2-6.
//  Copyright (c) 2013年 He Yuan. All rights reserved.
//

#import "TabBarViewController.h"
#import "MSAppDelegate.h"

@interface TabBarViewController ()
{
    UIView *customTabBar;
    
    UIButton *lastClickBtn;
}

@property(strong,nonatomic)UIView *customTabBar;

@end

@implementation TabBarViewController
@synthesize customTabBar = _customTabBar;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)loadView
{
    [super loadView];
    
    self.delegate = self;
    
    CGRect rect = self.tabBar.frame;
    self.customTabBar = [[UIView alloc]initWithFrame:rect];
    self.customTabBar.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
    [self.view addSubview:self.customTabBar];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

-(void)viewDidLayoutSubviews
{    
    [super viewDidLayoutSubviews];
}

-(void)viewWillLayoutSubviews
{
    self.customTabBar.frame = self.tabBar.frame;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    lastClickBtn = nil;
    
    CGFloat offset_x = 0.0f;
    
    for (int count = 0 ; count < 4; count++)
    {
        UIImage *tabImg = [UIImage imageNamed:[NSString stringWithFormat:@"tab_%d",count+1]];
        UIImage *tabImgPressed = [UIImage imageNamed:[NSString stringWithFormat:@"tab_%d_highlighted",count+1]];
        CGRect tabRect = CGRectMake(offset_x,0, tabImg.size.width, tabImg.size.height);
        offset_x += tabImg.size.width;
        if (count == 1)offset_x += tabImg.size.width;
        UIButton *tab = [[UIButton alloc] initWithFrame:tabRect];
        [tab setTag:count];
        [tab addTarget:self action:@selector(tabClick:) forControlEvents:UIControlStateHighlighted];
        [tab setBackgroundImage:tabImg forState:UIControlStateNormal];
        [tab setBackgroundImage:tabImgPressed forState:UIControlStateHighlighted];
        [self.customTabBar addSubview:tab];
        if (lastClickBtn==nil)[self tabClick:tab];
    }
    
    UIImage *buttonImage = [UIImage imageNamed:@"tab_photo"];
    UIButton *takePhotoButton  =  [UIButton buttonWithType:UIButtonTypeCustom];
    [takePhotoButton setFrame:CGRectMake([[UIScreen mainScreen] bounds].size.width/2-buttonImage.size.width/2,-6, buttonImage.size.width, buttonImage.size.height)];
	[takePhotoButton setShowsTouchWhenHighlighted:YES];
	[takePhotoButton addTarget:self action:@selector(addNewPhoto:) forControlEvents:UIControlStateHighlighted];
	[takePhotoButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
	[self.customTabBar addSubview:takePhotoButton];
}

-(void)tabClick:(UIButton *)btn
{
    if (btn != lastClickBtn)
    {
        UIImage *tabImg = [UIImage imageNamed:[NSString stringWithFormat:@"tab_%d",lastClickBtn.tag+1]];
        UIImage *tabImgPressed = [UIImage imageNamed:[NSString stringWithFormat:@"tab_%d_highlighted",lastClickBtn.tag+1]];
        [lastClickBtn setBackgroundImage:tabImg forState:UIControlStateNormal];
        [lastClickBtn setBackgroundImage:tabImgPressed forState:UIControlStateHighlighted];
        UIImage *tabImgPressednew = [UIImage imageNamed:[NSString stringWithFormat:@"tab_%d_highlighted",btn.tag+1]];
        [btn setBackgroundImage:tabImgPressednew forState:UIControlStateNormal];
        lastClickBtn = btn;
    }
    self.selectedIndex = btn.tag;
}

-(void)setCustomTabBarHidden:(BOOL)hidden
{
    CGRect r = self.customTabBar.frame;
    CGFloat offset_x =hidden?-r.size.width:0.0f;
    CGFloat duration = 0.345;
    self.customTabBar.hidden = NO;
    [UIView animateWithDuration:duration animations:^(void){
        CGRect rect = self.customTabBar.frame;
        rect.origin.x = offset_x;
        self.customTabBar.frame = rect;
    } completion:^(BOOL f){
        self.customTabBar.hidden = hidden;
    }];
}


//添加新的照片
- (void)addNewPhoto:(UIButton*)btn
{	
    [[MSAppDelegate sharedAppDelegate]showNewPicture];
}


-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIDeviceOrientation)orientation{
    if(orientation == UIDeviceOrientationPortrait) return NO;
    return NO;
}

@end











































